#include "SingleHandTrack.h"
#include "stdio.h"

SingleHandData::SingleHandData(){
	//Nui_Zero();
	//Nui_Init();
}
SingleHandData::~SingleHandData(){
	NuiShutdown();
}
void SingleHandData::Nui_Zero()
{
   /* if (m_pNuiSensor)
    {
        m_pNuiSensor->Release();
        m_pNuiSensor = NULL;
    }*/
    m_hNextDepthFrameEvent = NULL;
    m_hNextColorFrameEvent = NULL;
    m_hNextSkeletonEvent = NULL;
    m_pDepthStreamHandle = NULL;
    m_pVideoStreamHandle = NULL;
    m_hThNuiProcess = NULL;
    m_hEvNuiProcessStop = NULL;
    //ZeroMemory(m_Pen,sizeof(m_Pen));
    m_SkeletonDC = NULL;
    m_SkeletonBMP = NULL;
    m_SkeletonOldObj = NULL;
    m_PensTotal = 6;
   // ZeroMemory(m_Points,sizeof(m_Points));
    m_LastSkeletonFoundTime = 0;
    m_bScreenBlanked = false;
    m_DepthFramesTotal = 0;
    m_LastDepthFPStime = 0;
    m_LastDepthFramesTotal = 0;
   // ZeroMemory(m_SkeletonIds,sizeof(m_SkeletonIds));
    //ZeroMemory(m_TrackedSkeletonIds,sizeof(m_SkeletonIds));
}
HRESULT SingleHandData::Nui_Init()
{
	HRESULT hr;
	 /*if ( !m_pNuiSensor )
    {
        HRESULT hr = NuiCreateSensorByIndex(0, &m_pNuiSensor);

        if ( FAILED(hr) )
        {
            return hr;
        }

        SysFreeString(m_instanceId);

        m_instanceId = m_pNuiSensor->NuiDeviceConnectionId();
    }*/
	m_hNextSkeletonEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
	DWORD nuiFlags = NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON |  NUI_INITIALIZE_FLAG_USES_COLOR;
    hr = m_pNuiSensor->NuiInitialize(nuiFlags);
    if (E_NUI_SKELETAL_ENGINE_BUSY == hr)
    {
        nuiFlags = NUI_INITIALIZE_FLAG_USES_DEPTH |  NUI_INITIALIZE_FLAG_USES_COLOR;
        hr = m_pNuiSensor->NuiInitialize(nuiFlags);
    }
	hr = m_pNuiSensor->NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, 0 );
	m_hEvNuiProcessStop=CreateEvent(NULL,FALSE,FALSE,NULL);
    m_hThNuiProcess=CreateThread(NULL,0,Nui_ProcessThread,this,0,NULL);
    return hr;  

}
void SingleHandData::Nui_GotSkeletonAlert( )
  {
    NUI_SKELETON_FRAME SkeletonFrame = {0};

    bool bFoundSkeleton = false;

    if( SUCCEEDED(m_pNuiSensor->NuiSkeletonGetNextFrame( 0, &SkeletonFrame )) )
    {
      for( int i = 0 ; i < NUI_SKELETON_COUNT ; i++ )
      {
        if( SkeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED ||
        (SkeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_POSITION_ONLY && m_bAppTracking))
        {
          bFoundSkeleton = true;
      }
    }
  }
	if( !bFoundSkeleton )
    {
        return;
    }
	 // smooth out the skeleton data
    HRESULT hr = m_pNuiSensor->NuiTransformSmooth(&SkeletonFrame,NULL);
    if ( FAILED(hr) )
    {
        return;
    }
	for( int i = 0 ; i < NUI_SKELETON_COUNT ; i++ )
      {
		if( SkeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED &&
			SkeletonFrame.SkeletonData[i].eSkeletonPositionTrackingState[NUI_SKELETON_POSITION_SHOULDER_CENTER] != NUI_SKELETON_POSITION_NOT_TRACKED)
			{
				printf("Skeleton Data is :: %d ",SkeletonFrame.SkeletonData[i].SkeletonPositions[6]);
			}
		}

}
DWORD WINAPI SingleHandData::Nui_ProcessThread(LPVOID pParam)
{
    SingleHandData *pthis = (SingleHandData *) pParam;
    return pthis->Nui_ProcessThread();
}
DWORD WINAPI SingleHandData::Nui_ProcessThread()
{
	const int numEvents = 2;
    HANDLE hEvents[numEvents] = { m_hEvNuiProcessStop,m_hNextSkeletonEvent };
    int    nEventIdx;
    DWORD  t;
	m_LastSkeletonFoundTime = 0;
	bool continueProcessing = true;
    while ( continueProcessing )
    {
        // Wait for any of the events to be signalled
        nEventIdx = WaitForMultipleObjects( numEvents, hEvents, FALSE, 100 );

        // Process signal events
        switch ( nEventIdx )
        {
            case WAIT_TIMEOUT:
                continue;

			case WAIT_OBJECT_0:
                continueProcessing = false;
                continue;
			case WAIT_OBJECT_0 + 1:
                Nui_GotSkeletonAlert( );
                break;
        }
	}
	return 0;
}
